package com.xhl.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.xhl.service.OrderItemService;
import com.xhl.service.ProductImageService;
import com.xhl.service.ProductService;
import com.xhl.service.PropertyService;
import com.xhl.service.PropertyValueService;
import com.xhl.service.ReviewService;
import com.xhl.util.SpringContextUtil;
import com.xhl.util.Utils;
import com.xhl.vo.Pager;
import com.xhl.vo.ProductImageVO;
import com.xhl.vo.ProductVO;
import com.xhl.vo.PropertyVO;
import com.xhl.vo.PropertyValueVO;
import com.xhl.vo.ReviewVO;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    ProductService productService;

    @Autowired
    ProductImageService productImageService;

    @Autowired
    PropertyService propertyService;

    @Autowired
    OrderItemService orderItemService;

    @Autowired
    PropertyValueService propertyValueService;

    @Autowired
    ReviewService reviewService;

    @GetMapping(value = "/queryFuzzyName", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String queryFuzzyName(HttpServletRequest request) {
        String name = request.getParameter("name");

        List<ProductVO> pvolist = productService.queryFuzzyName(name);

        String json = JSON.toJSONString(pvolist);
        return json;
    }

    @GetMapping(value = "/productPid", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String queryProductPid(HttpServletRequest request) {
        String pid = request.getParameter("pid");
        if (pid == null) {
            return "异常";
        }
        System.out.println("-------------pid" + pid);
        ProductVO pvo = productService.queryProductPid(pid);
        List<ProductImageVO> pivolist = productImageService.queryProductImage(pid, null);
        pvo.setPivolist(pivolist);

        List<ReviewVO> rvolist = reviewService.queryReviewPid(pid);
        pvo.setRvolist(rvolist);

        String orderSUM = orderItemService.orderSUM(pid);
        pvo.setOrderSUM(orderSUM == null ? "0" : orderSUM);


        List<PropertyVO> ptvolist = propertyService.queryPropertyById(pvo.getCid() + "");
        for (PropertyVO ptvo : ptvolist) {
            PropertyValueVO pvvo = propertyValueService.queryPropertyValueById(pid, ptvo.getId() + "");
            PropertyValueVO pvvoNew = SpringContextUtil.getBean(PropertyValueVO.class);
            pvvoNew.setPid(Integer.valueOf(pid));
            pvvoNew.setPtid(ptvo.getId());
            ptvo.setPvvo(pvvo != null ? pvvo : pvvoNew);
        }
        pvo.setPtvolist(ptvolist);
        String json = JSON.toJSONString(pvo);
        return json;
    }


    @PostMapping(value = "/setCategory", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String setCategory(HttpServletRequest request) {
        String id = request.getParameter("id");
        String pid = request.getParameter("pid");
        String ptid = request.getParameter("ptid");
        String value = request.getParameter("value");
        System.out.println("修改product setCategory----------------" + id);
        if (id == null) {
            return "";
        }
        PropertyValueVO pvvo = SpringContextUtil.getBean(PropertyValueVO.class);
        pvvo.setId(Integer.valueOf(id));
        pvvo.setPid(Integer.valueOf(pid));
        pvvo.setPtid(Integer.valueOf(ptid));
        pvvo.setValue(value);

        if (pvvo.getId() == -1 && !value.isEmpty()) {
            System.out.println("添加add");
            propertyValueService.addPropertyValue(pvvo);
        } else if (pvvo.getId() != -1) {
            System.out.println("修改update");
            propertyValueService.updatePropertyValue(pvvo);
        }

        System.out.println("pvvo:   " + pvvo);
        return "修改成功";
    }

    @GetMapping(value = "/getCategory", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getCategory(HttpServletRequest request) {
        String cid = request.getParameter("cid");
        String pid = request.getParameter("pid");
        if (cid == null) {
            return "异常";
        }
        List<PropertyVO> plist = propertyService.queryPropertyById(cid);
        for (PropertyVO pvo : plist) {
            PropertyValueVO pvvo = propertyValueService.queryPropertyValueById(pid, pvo.getId() + "");
            PropertyValueVO pvvoNew = SpringContextUtil.getBean(PropertyValueVO.class);
            pvvoNew.setPid(Integer.valueOf(pid));
            pvvoNew.setPtid(pvo.getId());
            pvo.setPvvo(pvvo != null ? pvvo : pvvoNew);
        }

        String json = JSON.toJSONString(plist);
        return json;
    }


    @PostMapping(value = "/update", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateProduct(HttpServletRequest request) {

        String pid = request.getParameter("pid");
        String name = request.getParameter("name");
        String subTitle = request.getParameter("subTitle");
        String orignalPrice = request.getParameter("orignalPrice");
        String promotePrice = request.getParameter("promotePrice");
        String stock = request.getParameter("stock");
        System.out.println("修改product update----------------" + orignalPrice);
        if (pid == null) {
            return "";
        }
        if (orignalPrice == null) {
            return "";
        }

        ProductVO pvo = SpringContextUtil.getBean(ProductVO.class);
        pvo.setName(name);
        pvo.setSubTitle(subTitle);
        pvo.setOrignalPrice(Double.valueOf(orignalPrice));
        pvo.setPromotePrice(Double.valueOf(promotePrice));
        pvo.setStock(Long.valueOf(stock));
        pvo.setPid(Integer.valueOf(pid));

        productService.updateProduct(pvo);
        return "修改成功";
    }


    @GetMapping(value = "/delete", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String deleteProduct(HttpServletRequest request) {
        String id = request.getParameter("id");
        if (id == null) {
            return "异常";
        }
        productService.deleteProduct(id);
        return "删除成功";
    }


    @PostMapping(value = "/add", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String addProduct(HttpServletRequest request) {
        String cid = request.getParameter("cid");
        String name = request.getParameter("name");
        String subTitle = request.getParameter("subTitle");
        String orignalPrice = request.getParameter("orignalPrice");
        String promotePrice = request.getParameter("promotePrice");
        String stock = request.getParameter("stock");
        System.out.println("添加 orignalPrice ----------------" + orignalPrice);

        if (cid == null) {
            return "";
        }

        if (orignalPrice == null) {
            return "";
        }

        ProductVO pvo = SpringContextUtil.getBean(ProductVO.class);
        pvo.setName(name);
        pvo.setSubTitle(subTitle);
        pvo.setOrignalPrice(Double.valueOf(orignalPrice));
        pvo.setPromotePrice(Double.valueOf(promotePrice));
        pvo.setStock(Long.valueOf(stock));
        pvo.setCid(Integer.valueOf(cid));
        System.out.println(pvo);
        productService.addProduct(pvo);
        return "添加成功";
    }

    // 图片显示
    @GetMapping("/getImageSingle")
    @ResponseBody
    public void getSingle(HttpServletRequest rq, HttpServletResponse rp) {
        String pid = rq.getParameter("pid");
        if (pid == null) {
            return;
        }
        List<ProductImageVO> pilist = productImageService.queryProductImage(pid, "type_single");
        System.out.println(pilist.isEmpty() + " list  " + pilist);
        if (!pilist.isEmpty()) {
            ProductImageVO pivo = Utils.getLastElement(pilist);
            String filePath = "D:\\img\\productSingle\\" + pivo.getPiid() + ".jpg";
            Utils.showImg(rp, filePath);
        }


    }

    @GetMapping(value = "/query", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String queryProduct(HttpServletRequest request) {
        String cid = request.getParameter("cid");
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        if (cid == null) {
            return "异常";
        }

        Pager<ProductVO> p = new Pager<>();

        p.setSize(Integer.valueOf(pageSize));
        p.setPage((Integer.valueOf(pageNum) - 1) * Integer.valueOf(pageSize));
        p.setId(Integer.valueOf(cid));
        List<ProductVO> plist = productService.queryProduct(p);

        long count = productService.count(cid);
        p.setTotal(count);
        p.setRows(plist);
        String json = JSON.toJSONString(p);
        return json;
    }

}
